﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Data;
namespace NJL.WebManage.Company
{
    public partial class CompanyManage : BasePage
    {
        private readonly NJL.BLL.NJL_COMPANY bll = new BLL.NJL_COMPANY();
        private readonly NJL.BLL.NJL_COMPANY_SERVICE bllService = new BLL.NJL_COMPANY_SERVICE();
        private readonly BLL.NJL_CS_AREA bllArea = new BLL.NJL_CS_AREA();
        protected IList<Model.NJL_CS_AREA> ListArea;
        PagedDataSource pds;
        NJL.Model.NJL_COMPANY model;
        //NJL.Model.NJL_COMPANY_SERVICE modelService;

        protected string AreaCode = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["Page"] != null && Request["Page"] != "")
                {
                    ViewState["pager"] = Request["Page"];
                }
                else
                {
                    ViewState["pager"] = "0";
                }
                bind();
            }
        }

        private void LoadData()
        {
            ListArea = bllArea.GetModelList(" deep=1");
            if (Request.QueryString["key"] != null && Request.QueryString["key"] != "")
                this.txtKey.Text = Server.UrlDecode(Request.QueryString["key"]);
            if (!string.IsNullOrEmpty(Request["Claim"]))
            {
                ddlClaim.SelectedValue = Request["Claim"];
            }
            if (!string.IsNullOrEmpty(Request["Book"]))
            {
                ddlBook.SelectedValue = Request["Book"];
            }
        }

        public void bind()
        {
            LoadData();
            litTitle.Text = Request.QueryString["typeName"].ToString();
            txtPage.Text = (Convert.ToInt32(ViewState["pager"]) + 1).ToString();

            if (Request.QueryString["areacode"] != null && Request.QueryString["areacode"] != "")
            {
                AreaCode = Request.QueryString["areacode"];
            }
            pds = new PagedDataSource();
            pds.DataSource = bll.GetListManage(GetSelectWhere()).Tables[0].DefaultView;

            pds.AllowPaging = true;
            pds.PageSize = 15;
            pds.CurrentPageIndex = Convert.ToInt32(ViewState["pager"]);
            ViewState["endpage"] = pds.PageCount;
            lbtnFirst.Enabled = true;
            lbtnPrev.Enabled = true;
            lbtnNext.Enabled = true;
            lbtnEnd.Enabled = true;
            if (pds.IsFirstPage)
            {
                lbtnFirst.Enabled = false;
                lbtnPrev.Enabled = false;
            }
            if (pds.IsLastPage)
            {
                lbtnNext.Enabled = false;
                lbtnEnd.Enabled = false;
            }

            lit.Text = bll.GetListManageCount(GetSelectWhere()).ToString();

            litAllPage.Text = pds.PageCount.ToString();
            litCurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();
            rptUserList.DataSource = pds;
            rptUserList.DataBind();
        }
        protected void rptUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string id = e.CommandArgument.ToString();
                model = bll.GetModel(id);
                if (model != null)
                {
                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件
                }
                bllService.DeleteByCompanyID(id);
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
            else if (e.CommandName == "imgbtn1")
            {
                string id = e.CommandArgument.ToString();
                NJL.Model.NJL_COMPANY model = bll.GetModel(id);
                if (model != null)
                {
                    if (model.SHOW_STATE == 1)
                    {
                        model.SHOW_STATE = 0;
                    }
                    else
                    {
                        model.SHOW_STATE = 1;
                    }
                }
                model.ID = id;
                if (bll.Update(model))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "显示状态失败");
                }
            }
            else if (e.CommandName == "imgbtn2")
            {
                string id = e.CommandArgument.ToString();
                NJL.Model.NJL_COMPANY model = bll.GetModel(id);
                NJL.BLL.NJL_USER bllUser = new BLL.NJL_USER();
                NJL.Model.NJL_USER modelUser = bllUser.GetModel(model.NJL_USER_ID);
                if (model != null)
                {
                    if (model.AUDIT_STATE == 1)
                    {
                        if (modelUser != null)
                        {
                            modelUser.TYPE_CODE = "2";
                            if (bllUser.Update(modelUser))
                            { }
                        }
                        model.AUDIT_STATE = 0;
                    }
                    else
                    {
                        model.AUDIT_STATE = 1;
                        // 更新用户状态
                        if (modelUser != null)
                        {
                            modelUser.TYPE_CODE = "3";
                            if (bllUser.Update(modelUser))
                            { }
                        }
                    }
                }
                model.ID = id;
                if (bll.Update(model))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "审核状态设置失败");
                }
            }
            else if (e.CommandName == "imgbtnClaim")
            {
                string id = e.CommandArgument.ToString();
                NJL.Model.NJL_COMPANY model = bll.GetModel(id);
                if (model.CLAIM_STATE == 1)
                {
                    model.CLAIM_STATE = 0;
                }
                else
                {
                    model.CLAIM_STATE = 1;
                }
                if (bll.Update(model))
                {
                    bind();
                }
            }

        }
        protected void rptUserList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal lit = e.Item.FindControl("litNum") as Literal;
                lit.Text = ((e.Item.ItemIndex + 1) + (Convert.ToInt32(ViewState["pager"]) + 1) * 15 - 15).ToString();
                DataRowView row = (DataRowView)e.Item.DataItem;
                string id = row["IMAGE_CODE"].ToString();
                string imgurl = GetImageUrl(id);
                Image img = e.Item.FindControl("Image1") as Image;
                ViewState["iamgeid"] = "";
                if (imgurl != "")
                {
                    img.ImageUrl = imgurl;
                    ViewState["iamgeid"] = imgurl;
                }
                else
                {
                    img.Visible = false;
                }
                ImageButton imgbtn = e.Item.FindControl("imgbtn1") as ImageButton;
                if (imgbtn.AlternateText == "1")
                {
                    imgbtn.ImageUrl = "~/Images/true.gif";
                }
                else if (imgbtn.AlternateText == "0")
                {
                    imgbtn.ImageUrl = "~/Images/false.gif";
                }
                ImageButton imgbtn2 = e.Item.FindControl("imgbtn2") as ImageButton;
                if (imgbtn2.AlternateText == "1")
                {
                    imgbtn2.ImageUrl = "~/Images/true.gif";
                }
                else if (imgbtn2.AlternateText == "0")
                {
                    imgbtn2.ImageUrl = "~/Images/false.gif";
                }
                ImageButton imgbtnClaim = e.Item.FindControl("imgbtnClaim") as ImageButton;
                if (imgbtnClaim.AlternateText == "1")
                {
                    imgbtnClaim.ImageUrl = "~/Images/true.gif";
                }
                else
                {
                    imgbtnClaim.ImageUrl = "~/Images/false.gif";
                }
            }
        }
        protected void lbtnFirst_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = "0";
            bind();
        }
        protected void lbtnPrev_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) - 1;
            bind();
        }
        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) + 1;
            bind();
        }
        protected void lbtnEnd_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["endpage"]) - 1;
            bind();
        }
        protected void btnDel_Click1(object sender, EventArgs e)
        {
            string[] cbxid = HSelectId.Value.Split(',');
            foreach (string id in cbxid)
            {
                model = bll.GetModel(id);
                if (model != null)
                {
                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件
                }
                bllService.DeleteByCompanyID(id);          //删除服务信息
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }
        private static Regex RegNumber = new Regex("^[0-9]+$");
        protected void btnRir_Click(object sender, EventArgs e)
        {
            Match mc = RegNumber.Match(txtPage.Text);
            if (mc.Success && txtPage.Text != "0")
            {
                ViewState["pager"] = Convert.ToInt32(txtPage.Text) - 1;
                bind();
            }
        }

        /// <summary>
        /// 根据地区编号或网站编号获取信息统计数据
        /// </summary>
        /// <param name="areaCode"></param>
        /// <returns></returns>
        protected int GetListCountByAreaCode(string areaCode, string showState)
        {
            //只有总站管理员才能看到统计数据
            if (USER_TYPE_CODE == "1")
            {
                if (showState != "")
                {
                    return bll.GetListManageCount(GetSelectWhereCount() + " and NJL_COMPANY.NJL_SYS_BASE_ID = '" + areaCode + "' and NJL_COMPANY.SHOW_STATE=" + showState);
                }
                else
                {
                    return bll.GetListManageCount(GetSelectWhereCount() + " and NJL_COMPANY.NJL_SYS_BASE_ID = '" + areaCode + "'");
                }
            }
            else
            {
                //
                return bll.GetListManageCount(GetSelectWhereCount() + " and NJL_COMPANY.NJL_SYS_BASE_ID = '" + areaCode + "'");
            }
        }

        /// <summary>
        /// 获取当前页面数据查询条件
        /// </summary>
        /// <returns></returns>
        private string GetSelectWhere()
        {
            string strWhere = "";
            strWhere = "NJL_COMPANY.TYPE_CODE='" + Request.QueryString["typeid"] + "'";
            if (ddlClaim.SelectedValue != "-1")
            {
                strWhere += " and NJL_COMPANY.CLAIM_STATE=" + ddlClaim.SelectedValue;
            }
            if (WEBSITE_CODE == "1")
            {
                if (AreaCode != "")
                {
                    strWhere = strWhere + " and NJL_COMPANY.NJL_SYS_BASE_ID='" + AreaCode + "'";
                }
            }
            else
            {
                strWhere = strWhere + " and NJL_COMPANY.NJL_SYS_BASE_ID='" + WEBSITE_CODE + "'";
            }
            if (Request.QueryString["key"] != null && Request.QueryString["key"] != "")
            {
                strWhere = strWhere + " and NJL_COMPANY.COMPANY_NAME like '%" + Server.UrlDecode(Request.QueryString["key"]) + "%' ";
            }
            if (!string.IsNullOrEmpty(Request["Claim"]))
            {
                strWhere += " and NJL_COMPANY.CLAIM_STATE=" + Request["Claim"];
            }
            if (!string.IsNullOrEmpty(Request["Book"]))
            {
                strWhere += " and NJL_COMPANY.IS_OPEN_BOOK=" + Request["Book"];
            }
            return strWhere;
        }

        /// <summary>
        /// 获取当前页面统计数据的条件(把区域判断去掉了)
        /// </summary>
        /// <returns></returns>
        private string GetSelectWhereCount()
        {
            string strWhere = "";

            strWhere = "NJL_COMPANY.TYPE_CODE='" + Request.QueryString["typeid"] + "'";

            if (WEBSITE_CODE != "1")
            {
                strWhere = strWhere + " and NJL_COMPANY.CREATE_USER_ID='" + USER_ID + "'";
            }

            if (USER_TYPE_CODE == "2")
            {
                strWhere = strWhere + " and NJL_COMPANY.SHOW_STATE=1 ";
            }
            if (Request.QueryString["key"] != null && Request.QueryString["key"] != "")
            {
                strWhere = strWhere + " and NJL_COMPANY.COMPANY_NAME like '%" + Server.UrlDecode(Request.QueryString["key"]) + "%' ";
            }
            if (!string.IsNullOrEmpty(Request["Claim"]))
            {
                strWhere += " and NJL_COMPANY.CLAIM_STATE=" + Request["Claim"];
            }
            if (!string.IsNullOrEmpty(Request["Book"]))
            {
                strWhere += " and NJL_COMPANY.IS_OPEN_BOOK=" + Request["Book"];
            }
            if (WEBSITE_CODE == "1")
            {
                if (AreaCode != "")
                {
                    strWhere = strWhere + " and NJL_COMPANY.NJL_SYS_BASE_ID='" + AreaCode + "'";
                }
            }
            return strWhere;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //
            string strKey = txtKey.Text.Trim();
            string strTypeID = Request.QueryString["typeid"];
            string strTypeName = Request.QueryString["typename"];

            string strURL = "CompanyManage.aspx?typeid=" + Request.QueryString["typeid"] + "&typename=" + Request.QueryString["typename"];

            if (Request.QueryString["areacode"] != null && Request.QueryString["areacode"] != "")
            {
                strURL += "&areacode=" + Request.QueryString["areacode"];
            }

            strURL += "&key=" + Server.UrlEncode(strKey);
            if (ddlClaim.SelectedValue != "-1")
            {
                strURL += "&Claim=" + ddlClaim.SelectedValue;
            }
            if (ddlBook.SelectedValue != "-1")
            {
                strURL += "&Book=" + ddlBook.SelectedValue;
            }
            Response.Redirect(strURL, true);
        }
    }
}